
const DB = require('../config/Sequelize')
const BackCode = require("../utils/BackCode");
const SecretTool = require('../utils/SecretTool');
const {Op} = require('sequelize');    
const BarrageService = {
    add: async (req) => {
        const {content,episodeId,playTime,productId} = req.body;
        if(!(content && episodeId && Number(playTime) >= 0 && productId)){
            return BackCode.buildError({msg:'参数错误'})
        }
        let token = req.headers.authorization.split(' ').pop();
        let userInfo = SecretTool.jwtVerify(token);
        let barrageItem = {
            episde_id: episodeId,
            product_id: productId,
            play_time: playTime,
            content: content,
            account_id: userInfo.id,
            head_img: userInfo.head_img,
            username: userInfo.username,
            del: 0,
        }
        await DB.BulletScreen.create(barrageItem);
        return BackCode.buildSuccess()
    },
    list_by_episode: async (req) => {
        let {beginTime,endTime,episodeId,productId} = req.body;
        if(!(Number(beginTime) >=0 && endTime && episodeId && productId)){
            return BackCode.buildError({msg:'参数错误'})
        }
        let barrageList = await DB.BulletScreen.findAll({
            where: {
                episode_id: episodeId,
                product_id: productId,
                play_time: {
                    [Op.between]: [beginTime,endTime]
                }
            }
        })
        console.log('barrageList: ', barrageList);
        return BackCode.buildSuccessAndData({data:barrageList})
    }
}
module.exports = BarrageService